﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
   public class ClsHolidaysMaster
    {
        public string HolidayName { get; set; }
        public int HolidaysMasterID { get; set; }


        public DataTable GetAllHolidayNames()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from HolidaysMaster", "HolidayName");
            }
        }
        public void GetHolidayName(int intHolidaysMasterID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from HolidaysMaster where HolidaysMasterID=" + intHolidaysMasterID.ToString(), "HolidayName");
                if (dt.Rows.Count > 0)
                {
                    HolidaysMasterID = Convert.ToInt32(dt.Rows[0]["HolidaysMasterID"]);
                    HolidayName = dt.Rows[0]["HolidayName"].ToString();
                }
            }
        }
        public bool SaveHolidayName(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from HolidaysMaster where HolidayName='" + HolidayName + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("HolidayName already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into HolidaysMaster (HolidayName) values('" + HolidayName + "')");
                    }

                }
                else
                {
                    string count = data.GetValue("select count(*) from HolidaysMaster where HolidayName='" + HolidayName + "' and HolidaysMasterID NOT IN (" + HolidaysMasterID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("HolidayName already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update HolidaysMaster set HolidayName = '" + HolidayName + "' where HolidaysMasterID=" + HolidaysMasterID.ToString());
                    }
                }
            }
            return true;
        }
        public void DeleteHolidayName()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  HolidaysMaster where HolidaysMasterID=" + HolidaysMasterID.ToString());
            }
        }

    }
}
